package com.qianfeng.ttgy.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.bitmap.PauseOnScrollListener;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.qianfeng.ttgy.R;
import com.qianfeng.ttgy.adapter.MainAdapter;
import com.qianfeng.ttgy.adapter.ViewPagerAdater;
import com.qianfeng.ttgy.bean.AppBean;
import com.qianfeng.ttgy.bean.Banner;
import com.qianfeng.ttgy.bean.Rotation;
import com.qianfeng.ttgy.bean.Top_banner;
import com.qianfeng.ttgy.constans.UrlConstans;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/8/4.
 */
public class MainFragment extends Fragment {
    private List<View> imgs = new ArrayList<>();
    @ViewInject(R.id.fragment_main_listView)
    private ListView mainListView;
    @ViewInject(R.id.fragment_main_viewPager)
    private ViewPager mainViewPager;
    private MainAdapter mainAdapter;
    private ViewPagerAdater mianViewPagerAdapter;
    private List<Banner>bannerList = new ArrayList<>();
    private View view;
    private BitmapUtils bitmapUtils;
    private List<Rotation>rotationList = new ArrayList<>();
    private HttpUtils httpUtils = new HttpUtils();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initBitmapUtils();

    }

    private void initBitmapUtils() {
        bitmapUtils = new BitmapUtils(getActivity());
        bitmapUtils.configDefaultLoadFailedImage(R.mipmap.ic_launcher);
        bitmapUtils.configDefaultLoadFailedImage(R.mipmap.ic_launcher);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_main,container,false);
        ViewUtils.inject(this, view);
        initView();
        initData();

        return view;
    }

    private void initData() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams requestParams = getRequestParams();

        httpUtils.send(HttpRequest.HttpMethod.POST, UrlConstans.Url_HOME, requestParams, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String banner = responseInfo.result;
                AppBean bean = JSONObject.parseObject(banner, AppBean.class);
                if (bean != null) {
                    if(bean.getBannerList()!=null&&bean.getBannerList().size()>0){
                        bannerList.addAll(bean.getBannerList());
                        mainAdapter.notifyDataSetChanged();
                    }
                    if(bean.getRotationList()!=null&&bean.getRotationList().size()>0){
                        addTopPic();
                    }
                }
            }


            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
       // addTopPic();

    }

    private RequestParams getRequestParams() {
        RequestParams requestParams = new RequestParams();
        requestParams.addBodyParameter("sign", "4b5c5b09495f149956d8fd790acc7969");
        requestParams.addBodyParameter("timestamp", "1438611225");
        requestParams.addBodyParameter("platform", "ANDROID");
        requestParams.addBodyParameter("source", "app");
        requestParams.addBodyParameter("connect_id", "");
        requestParams.addBodyParameter("region_id", "143949");
        requestParams.addBodyParameter("service", "marketing.banner");
        requestParams.addBodyParameter("device_id", "93789e8978f2eefdcf52006b4d96ee23");
        requestParams.addBodyParameter("channel", "qq");
        requestParams.addBodyParameter("version", "2.2.0");
        return requestParams;
    }

    private void initView() {
        mainAdapter = new MainAdapter(getActivity(),bannerList,bitmapUtils);
        mianViewPagerAdapter = new ViewPagerAdater(imgs);
        mainListView.setOnScrollListener(new PauseOnScrollListener(bitmapUtils, true, false));//参数什么意思
        mainListView.setAdapter(mainAdapter);
        mainViewPager.setAdapter(mianViewPagerAdapter);
    }
    private void addTopPic() {

        mainViewPager = (ViewPager) view.findViewById(R.id.fragment_main_viewPager);
        //在代码中动态添加viewpager中的图片
        for (int i = 0; i <3 ; i++){
            ImageView img = new ImageView(getActivity());
            img.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            img.setScaleType(ImageView.ScaleType.FIT_XY);
            //ImageLoader.getInstance().displayImage(list.get(i).getHotpic(), img, options);
            bitmapUtils.display(img,rotationList.get(i).getPhoto());
            imgs.add(img);
           // img.setOnClickListener(this);
        }
        mianViewPagerAdapter.notifyDataSetChanged();


    }
}
